package d0908;
import java.util.*;
import java.util.function.Consumer;

public class set集合 {
    public static void main(String[] args) {
        // Set 无序, 不重复
        HashSet<String> set1 = new HashSet<>();
        set1.add("张三");
        set1.add("李四");
        set1.add("李四");
        set1.add("李四");
        set1.add("王五");
        System.out.println(set1);
        // 匿名类 ==> lambda表达式
        System.out.println("---------------");
        set1.forEach(new Consumer<String>() {
            @Override
            public void accept(String t) {
                System.out.println("你好 " + t);
            }
        });
        System.out.println("---------------");
        set1.forEach((String t) -> {
            System.out.println("你好 " + t);
        });
        System.out.println("---------------");
        // lambda 表达式
        set1.forEach(t -> System.out.println("你好 " + t));

        System.out.println("==============================");
        //////////////  有序不重复   /////////////////
        LinkedHashSet<Student> set2 = new LinkedHashSet<>();
        set2.add(new Student(1,"武松",'男'));
        set2.add(new Student(1,"武松",'男'));
        set2.add(new Student(2,"武松",'男'));
        set2.add(new Student(3,"林冲",'男'));
        set2.add(new Student(4,"孙二娘",'女'));
        set2.forEach(t -> System.out.println(t));

        System.out.println("==============================");
        ////////////// 排序不重复   /////////////////
        TreeSet<Student> set3 = new TreeSet<>();
        set3.add(new Student(1,"武松",'男'));
        set3.add(new Student(4,"孙二娘",'女'));
        set3.add(new Student(1,"武松",'男'));
        set3.add(new Student(2,"武松",'男'));
        set3.add(new Student(3,"林冲",'男'));
        set3.forEach(t -> System.out.println(t));

    }

}

class Student implements Comparable<Student>{
    //录入以下属性学生信息
    private int id; //学号
    private String name; // 姓名
    private char gender; // 性别
    private boolean threeGood; // 三好学生
    private float height; // 身高
    private String phone; // 电话

    public Student(int id, String name, char gender) {
        super();
        this.id = id;
        this.name = name;
        this.gender = gender;
    }

    @Override
    public int compareTo(Student o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", threeGood=" + threeGood + ", height="
                + height + ", phone=" + phone + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + gender;
        result = prime * result + Float.floatToIntBits(height);
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((phone == null) ? 0 : phone.hashCode());
        result = prime * result + (threeGood ? 1231 : 1237);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (gender != other.gender)
            return false;
        if (Float.floatToIntBits(height) != Float.floatToIntBits(other.height))
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (phone == null) {
            if (other.phone != null)
                return false;
        } else if (!phone.equals(other.phone))
            return false;
        if (threeGood != other.threeGood)
            return false;
        return true;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public boolean isThreeGood() {
        return threeGood;
    }

    public void setThreeGood(boolean threeGood) {
        this.threeGood = threeGood;
    }

    public float getHeight() {
        return height;
    }

    public void setHeight(float height) {
        this.height = height;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

